package com.tanhua.recommend.msg;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.el.parser.AstEmpty;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;


@Component//将类加入到容器
@RocketMQMessageListener(topic = "tanhua-quanzi",
consumerGroup = "tanhua-quanzi-consumer")//指定topic等于监听它,和分组
@Slf4j
public class QuanZiMsgConsumer implements RocketMQListener<String> {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(String msg) {
        try {
            JsonNode jsonNode = MAPPER.readTree(msg);

            int type = jsonNode.get("type").asInt();
            String publishId = jsonNode.get("publishId").asText();
            long date = jsonNode.get("date").asLong();
            long userId = jsonNode.get("userId").asLong();
            long pid = jsonNode.get("pid").asLong();

            RecommendQuanZi recommendQuanZi = new RecommendQuanZi();
            recommendQuanZi.setPublishId(pid);
            recommendQuanZi.setDate(date);
            recommendQuanZi.setUserId(userId);
            recommendQuanZi.setId(ObjectId.get());

            //针对不同的条件处理
            switch (type){
                case  1:{
                    int score=0;
                    Publish publish = this.mongoTemplate.findById(new ObjectId(publishId), Publish.class);
                    int length = StringUtils.length(publish.getText());
                    if (length>0&&length<=50){
                        score+=1;
                    }else if (length>50&&length<=100){
                        score+=2;
                    }else if (length>100){
                        score+=3;
                    }

                    if (!CollectionUtils.isEmpty(publish.getMedias())){
                        //一张图片一分
                        score+=publish.getMedias().size();
                    }
                    recommendQuanZi.setScore(Double.valueOf(score));
                    break;
                }
                case  2:{
                    recommendQuanZi.setScore(1d);
                    break;
                }
                case  3:{
                    recommendQuanZi.setScore(5d);
                    break;
                }
                case  4:{
                    recommendQuanZi.setScore(8d);
                    break;
                }
                case  5:{
                    recommendQuanZi.setScore(10d);
                    break;
                }
                case  6:{
                    recommendQuanZi.setScore(-5d);
                    break;
                }
                case  7:{
                    recommendQuanZi.setScore(-8d);
                    break;
                }
                default:{
                    recommendQuanZi.setScore(0d);
                    break;
                }
            }
            String collectionName = "recommend_quanzi_" + new DateTime().toString("yyyyMMdd");
            this.mongoTemplate.save(recommendQuanZi,collectionName);
        } catch (Exception e) {
           log.error("消息处理失败 ! msg ="+msg);
        }
    }
}
